﻿/*!

 @Title: PicSign
 @Description：jQuery图片标注组件
 @Site: www.artlessbruin.cn
 @gitee：https://gitee.com/ArtlessBruin/PicSign
 @Author: ArtlessBruin
 @License：MIT
 http://111.231.227.116:2486
 @Version：1.0.0

 */
let posturl='http://127.0.0.1:2486'
var isnewcode=true
function getselect2(){
    
    $(".pic_line_div_select").unbind('change');
    $(".pic_line_div_select").change(function(){

        $(this).parent().children(".pic_line_div_select2").empty();

        var codeid = $(this).val();

        // console.log(codeid);
        
        
        var select2;
        
        for(var i=0;i<select1.length;i++){
            if(select1[i].codeId==codeid){
                select2 = select1[i].childCode;
            }
        }
        
        // console.log(select2);
        
        var opt = "<option value='undefined' selected style='display:none;'>请选择</option>";
        for(var i=0;i<select2.length;i++){
            opt += "<option value ='"+select2[i].codeId+"'>"+select2[i].codeName+"</option>";
        
        }
        
        // console.log(opt);
        
        $(this).parent().children(".pic_line_div_select2").append(opt);
        
        // console.log( $(this).parent().children(".pic_line_div_select2")  )
        
    
    })
}


var select1
(function ($) {
    $.ajax({
        type: "POST",
        url: posturl+'/code/findcodebysupcodeid',
        dataType: "json",
        data: {
            sup_code_id: '01',
            token:sessionStorage.getItem('token')

        },
        success: function (data) {
           select1=data
        //    console.log('select1',select1);
           
        },
        error: function (jqXHR) {
            alert("发生错误：" + jqXHR.status);
        }
    });
    //随机数 用于生成唯一标识
    function untrue_guid() {
        function S4() {
            return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
        }
        return S4() + S4() + S4() + S4();
    }
    //webuiPopover默认设置
    // var ts=this.newStore();
    var webuiPopoverSetting = {
        title: "",
        content: "",
        input1: "",
        input2: "",
        trigger: 'hover', //click
        multi: true
    };
    //picsign 默认设置
    var picsigndefaults = {
        picurl: null,
        signdata: [],
        editable: { //是否可编辑
            add: true, //是否可添加
            update: true, //是否可修改
            del: true, //是否可删除
            move: true //是否可移动
        },
        signclass: 'signdot',
        popwidth: 400,
        popheight: 247,
        inputwidth: 400,
        inputheight: 247,
        beforeadd: function (data) {},
        onadd: function (data) {},
        beforeupdate: function (data) {},
        onupdate: function (data) {},
        beforedel: function (data) {},
        ondel: function (data) {}
    };
    //开放调用的方法
    var allowedMethods = [
        'getData', //获取数据
        'addSign', //添加数据
        'toggle', //切换显示
        'destroy' //销毁
    ];
    //jQuery扩展
    $.fn.picsign = function (option) {
        var value;
        var args = Array.prototype.slice.call(arguments, 1);
        var options = $.extend({}, picsigndefaults, typeof option === 'object' && option);
        this.each(function () {
            var $this = $(this);
            var data = $this.data('jquery.picsign');
            if (typeof option === 'string') {
                if ($.inArray(option, allowedMethods) < 0) {
                    throw new Error("Unknown method: " + option);
                }
                if (!data) {
                    return;
                }
                value = data[option].apply(data, args);
                if (option === 'destroy') {
                    $this.removeData('jquery.picsign');
                }
            }
            if (!data) {
                $this.data('jquery.picsign', data = new PicSign(this, options));
                value = true;
            }
        });
        return value;
        //return typeof value === 'undefined' ? this : value;
    };
    //PicSign
    class PicSign {
        //构造函数
        constructor(el, options) {
            this.ID = untrue_guid();
            this.Options = options;
            this.$_el = $(el);
            this.$el = null;
            this.Data = [];
            this.signMoveId = null;
            this.Interval = null;
            this.init();
        }
        //初始化
        init() {
            this.dominit();
            this.datainit2();
        }
        //销毁
        destroy() {
            window.clearInterval(this.Interval);
            this.domget().webuiPopover('destroy');
            this.$el.remove();
            return true;
        }
        //DOM初始化
        dominit() {
            var $this = this;
            $this.$_el.html("");
            var img = $('<img class="picsignimg" style="width:100%" src="' + $this.Options.picurl + '" />');
            var div = $("<div class='picsign'></div>");
            $this.$_el.append(div);
            $this.$_el.append(img);
            $this.$el = $(div);
            $this.Interval = setInterval(function () {
                $this.$el.css('margin-left', $(img).position().left - $this.$el.position().left);
                $this.$el.css('margin-top', $(img).position().top - $this.$el.position().top);
                $this.$el.width($(img).width());
                $this.$el.height($(img).height());
            }, 10);

            var get_Btn_signid = function (btn) {
                if (btn) {
                    var signid = $(btn).data("signid");
                    return signid;
                }
                return null;
            };
            //获取位置
            var getlocation = function (e) {
                var signleft = e.clientX - ($this.$el.offset().left - $(document).scrollLeft());
                var signtop = e.clientY - ($this.$el.offset().top - $(document).scrollTop());
                var left = (signleft / $this.$el.width() * 100).toFixed(4) + "%";
                var top = (signtop / $this.$el.height() * 100).toFixed(4) + "%";
                return {
                    left: left,
                    top: top
                };


            };

            var prompt = function (success, msg) {


                layer.prompt({
                        title: '设备信息',
                        formType: 0,
                        width: 800,
                        height: $this.Options.inputheight,
                        value: "配电箱名称"
                    },
                    function (text, index) {
                        layer.close(index);

                        let msgdata = {};
                        msgdata.pdxname = text;
                        msgdata.sbname = $('#msg1').val();
                        msgdata.sbbh = $('#msg2').val();

                        msgdata.msg11 = [];
                        $('.pic_div3').each(function () {
                            var config = {};
                            config.select1 = $(this).children(".pic_line_div_select").find("option:selected").text();
                            config.select2 = $(this).children(".pic_line_div_select2").find("option:selected").text();
                            config.select1val = $(this).children(".pic_line_div_select").val();
                            config.select2val = $(this).children(".pic_line_div_select2").val();
                            config.paramCodeId = $(this).children(".pic_line_div_paramCodeId").val();
                            config.limitTop = $(this).children(".pic_line_div_limitTop").val();
                            config.limitLow = $(this).children(".pic_line_div_limitLow").val();

                            msgdata.msg11.push(config);
                        })

                        if (text.length > 0) {
                            if (success && typeof success === "function") {
                                setTimeout(() => {
                                    success(msgdata);
                                }, 500);
                                
                            }
                        }
                    });

                $(".layui-layer-content").append("<div class='pic_div1'><input type='text' id='msg1' placeholder='设备名称'/></div>");
                $(".layui-layer-content").append("<div class='pic_div2'><input type='text' id='msg2' placeholder='设备编号'/></div>");
                $(".layui-layer-content").append("<a  class='pic_div2_btn'>添加参数</a>");

                $('.pic_div2_btn').click(function () {

                    console.log(select1);
                    
                    var opt = "<option value='undefined' selected style='display:none;'>请选择</option>";
                    for (let i = 0; i < select1.length; i++) {
                        
                       opt +=  "<option value ='"+select1[i].codeId+"'>"+select1[i].codeName+"</option>";
                    }



                    $(".layui-layer-content").append(
                        "<div class = 'pic_div3 pic_line_div' > " +
                        "		<select class='pic_line_div_select'>" +
                                    opt +
                        "		</select>&nbsp;&nbsp;" +
                        "		<select class='pic_line_div_select2'>" +
                        "          <option value='undefined' selected style='display:none;'>请选择</option>"+
                        "		</select>&nbsp;&nbsp;" +
                        "		<span>设备端口号：</span><input class='pic_line_div_paramCodeId' type='text' class='tuozhuai_input'/>&nbsp;&nbsp;" +
                        "		<span>上限：</span><input class='pic_line_div_limitTop' type='text'  class='tuozhuai_input'/>&nbsp;&nbsp;" +
                        "		<span>下限：</span><input class='pic_line_div_limitLow' type='text' class='tuozhuai_input'/>&nbsp;&nbsp;" +
                        "		<button type='button' style='background:red;border:none;color:#fff;border-radius:3px;width:60px' class='btn_sc'>删除</button>" +
                        "</div>"
                    );


                    getselect2();

                    $('.btn_sc').click(function () {
                        // alert('haha')
                        $(this).parent().remove();
                    })

                    var deviceconfig = [];

                    $('.pic_div3').each(function () {
                        // console.log($(this).children(".pic_line_div_paramCodeId").val());
                        var config = {};

                    })
                })
            };
            //双击
            $this.$el.on("dblclick", function (e) {
                var location = getlocation(e);
                if ($this.Options.editable !== false && ($this.Options.editable === true || $this.Options.editable.add !== false)) {
                    prompt(function (text) {
                        $this.dataset({
                            left: location.left,
                            top: location.top,
                            msg: text
                        });
                    });
                }
                $this.movecancel();
            });
            //单击
            $this.$el.on("click", function (e) {
                var location = getlocation(e);
                if ($this.signMoveId) {
                    //移动

                    $this.dataset({
                        signid: $this.signMoveId,
                        left: location.left,
                        top: location.top
                    });
                }
                $this.movecancel();
            });
            //删除
            $(document).on('click', '.picsign_bdel_' + $this.ID, function () {
                var signid = get_Btn_signid($(this));
                if (signid) {
                    WebuiPopovers.hideAll();
                    $this.datadel(signid);
                }
            });
            //移动
            $(document).on('click', '.picsign_bmove_' + $this.ID, function () {
                var signid = get_Btn_signid($(this));
                if (signid) {
                    WebuiPopovers.hideAll();
                    $this.signMoveId = signid;
                    $this.$el.css("cursor", "move");
                }
            });
            //编辑
            $(document).on('click', '.picsign_bedit_' + $this.ID, function () {

                setTimeout(() => {
                    // alert('domclick')

                    
                    for (let i = 0; i < signdata.msg.msg11.length; i++) {
                        
                        var ele = signdata.msg.msg11[i];
                        
                        var opt = "<option value='undefined' selected style='display:none;'>请选择</option>";
                        for (let j = 0; j < select1.length; j++) {
                            if(ele.select1val==select1[j].codeId){
                                opt +=  "<option value ='"+select1[j].codeId+"' selected>"+select1[j].codeName+"</option>";
                            }else{
                                opt +=  "<option value ='"+select1[j].codeId+"'>"+select1[j].codeName+"</option>";
                            }
                           
                        }

                        var select2;
                        for(var j=0;j<select1.length;j++){
                            if(select1[j].codeId==ele.select1val){
                                select2 = select1[j].childCode;
                            }
                        }
                        
                        var opt2 = "<option value='undefined' selected style='display:none;'>请选择</option>";
                        for(var j=0;j<select2.length;j++){
                            if(ele.select2val==select2[j].codeId){
                                opt2 += "<option value ='"+select2[j].codeId+"' selected>"+select2[j].codeName+"</option>";
                            }else{
                                opt2 += "<option value ='"+select2[j].codeId+"'>"+select2[j].codeName+"</option>";
                            }
                        
                        }

                        $(".layui-layer-content").append(
                            "<div class = 'pic_div3 pic_line_div' > " +
                            "		<select class='pic_line_div_select'>" +
                                            opt+
                            "		</select>&nbsp;&nbsp;" +
                            "		<select class='pic_line_div_select2'>" +
                                        opt2+
                            "		</select>&nbsp;&nbsp;" +
                            "		<span>设备端口号：</span><input class='pic_line_div_paramCodeId' type='text' class='tuozhuai_input' value='" + ele.paramCodeId + "' />&nbsp;&nbsp;" +
                            "		<span>上限：</span><input class='pic_line_div_limitTop' type='text'  class='tuozhuai_input' value='" + ele.limitTop + "' />&nbsp;&nbsp;" +
                            "		<span>下限：</span><input class='pic_line_div_limitLow' type='text' class='tuozhuai_input' value='" + ele.limitLow + "' />&nbsp;&nbsp;" +
                            "		<button type='button' style='background:red;border:none;color:#fff;border-radius:3px;width:60px' class='btn_sc2'>删除</button>" +
                            "</div>"
                        );
                        $('.btn_sc2').click(function () {
                            // alert('haha')
                            $(this).parent().remove();
                        })
                        

                    }


                    getselect2()

                }, 500);



                var signid = get_Btn_signid($(this));


                if (signid) {
                    WebuiPopovers.hideAll();

                    var signdata = $this.dataget(signid);
                    // console.log('signdata', signdata);




                    prompt(function (text) {

                        $this.dataset({
                            signid: signdata.signid,
                            msg: text
                        });
                    }, signdata.msg.pdxname);

                    // console.log($('#msg2'))
                    $('#msg2').attr('disabled','disabled')
                    $('.layui-layer-input').val(signdata.msg.pdxname);
                    $('#msg1').val(signdata.msg.sbname);
                    $('#msg2').val(signdata.msg.sbbh);

                }
            });
        }
        //DOM添加
        domadd(signdata) {
            // alert('domadd')
            // console.log('domaddsigndata',signdata);
            
            var $this = this;
            if (signdata) {
                var sign = $("<div class='" + $this.Options.signclass + "' data-signid='" + signdata.signid + "'></div>");
                $this.$el.append(sign);
                sign.css({
                    "left": signdata.left,
                    "top": signdata.top,
                    "display": "block"
                });
                //同步显示
                $this.domget().hide();
                setTimeout(function () {
                    $this.domget().show();
                }, 10);

                
                console.log('signdata', signdata);
                var msg11arr=[];
                
                for (let i = 0; i < signdata.msg.msg11.length; i+=1) {
                    var msg11obj={}
                    msg11obj.dataCodeId= signdata.msg.msg11[i].paramCodeId
                    msg11obj.paramCodeId= signdata.msg.msg11[i].select2val
                    msg11obj.limitLow= signdata.msg.msg11[i].limitLow
                    msg11obj.limitTop= signdata.msg.msg11[i].limitTop
                    msg11arr.push(msg11obj)
                }
                console.log('msg11arr',msg11arr);
                
                //新增接口****************************************
                $.ajax({
                    type: "POST",
                    url: posturl+'/device/adddevice',
                    dataType: "json",
                    data: {
                        regionid: sessionStorage.getItem('id'),
                        deviceName:signdata.msg.sbname,
                        deviceId:signdata.msg.sbbh,
                        powerDistName:signdata.msg.pdxname,
                        sideX:signdata.left,
                        sideY:signdata.top,
                        dcJson:JSON.stringify(msg11arr),
                        token:sessionStorage.getItem('token')
                    },
                    success: function (data) {
                        // console.log(data);
                        if (data.res=="true") {
                            $this.dompopover(signdata, true);
                            layer.msg('添加成功！');
                            
                        }else{
                            // console.log(signdata.signid);
                            for (var i = 0; i < $this.Data.length; i++) {
                                if ($this.Data[i].signid === signdata.signid) {
           
                                    $this.domdel(signdata.signid);
                                    $this.event("ondel", $this.Data[i]);
                                    $this.Data.splice(i, 1);

                                }
                            }
                            $this.datadel(signdata.signid);
                            layer.msg(data.msg);
                            
                        }
  
                       
                    },
                    error: function (jqXHR) {
                        alert("发生错误：" + jqXHR.status);
                    }
                });
            }
        }
                //DOM添加
        domadd2(signdata) {
            // console.log(signdata);
            
            var $this = this;
            if (signdata) {
                var sign = $("<div class='" + $this.Options.signclass + "' data-signid='" + signdata.signid + "'></div>");
                $this.$el.append(sign);
                sign.css({
                    "left": signdata.left,
                    "top": signdata.top,
                    "display": "block"
                });
                //同步显示
                $this.domget().hide();
                setTimeout(function () {
                    $this.domget().show();
                }, 10);

                $this.dompopover(signdata, true);
            }
        }
        //设置和更新popover
        dompopover(signdata, isadd) {
            var $this = this;
            if (signdata) {
                var sign = $this.domget(signdata.signid);
                if (sign) {
                    if (!isadd) {
                        // console.log('signdata', signdata);
                        var line2 = '';
                        for (let i = 0; i < signdata.msg.msg11.length; i += 1) {
                            line2 += '设备端口号：' + signdata.msg.msg11[i].paramCodeId + ' ; 上限：' + signdata.msg.msg11[i].limitTop + ' ; 下限：' + signdata.msg.msg11[i].limitLow + '<br/>'
                        }
                        // console.log(signdata);
                        WebuiPopovers.updateContent(sign, "配电箱名称：" + signdata.msg.pdxname + '<br/>' + "设备名称：" + signdata.msg.sbname + '<br/>' + "设备编号:" + signdata.msg.sbbh + '<br/>' + line2);
                        //编辑&&移动存数据******************************
                        var msg11arr=[];
                        
                        for (let i = 0; i < signdata.msg.msg11.length; i+=1) {
                            var msg11obj={};
                            msg11obj.dataCodeId= signdata.msg.msg11[i].paramCodeId
                            msg11obj.paramCodeId= signdata.msg.msg11[i].select2val
                            msg11obj.limitLow= signdata.msg.msg11[i].limitLow
                            msg11obj.limitTop= signdata.msg.msg11[i].limitTop
                            msg11arr.push(msg11obj)
                        }
                        $.ajax({
                            type: "POST",
                            url: posturl+'/device/updatedevice',
                            dataType: "json",
                            data: {
                                regionid: sessionStorage.getItem('id'),
                                deviceName:signdata.msg.sbname,
                                deviceId:signdata.msg.sbbh,
                                powerDistName:signdata.msg.pdxname,
                                sideX:signdata.left,
                                sideY:signdata.top,
                                dcJson:JSON.stringify(msg11arr),
                                token:sessionStorage.getItem('token')
                    
                            },
                            success: function (data) {
                                // console.log(data);
                                if (data.res=="true") {
                                    $this.dompopover(signdata, true);
                                    layer.msg('修改成功！');
                                    
                                }
                                // else{
                                //     console.log(signdata.signid);
                                //     for (var i = 0; i < $this.Data.length; i++) {
                                //         if ($this.Data[i].signid === signdata.signid) {
                   
                                //             $this.domdel(signdata.signid);
                                //             $this.event("ondel", $this.Data[i]);
                                //             $this.Data.splice(i, 1);
        
                                //         }
                                //     }
                                //     $this.datadel(signdata.signid);
                                //     layer.msg(data.msg);
                                    
                                // }
          
                               
                            },
                            error: function (jqXHR) {
                                alert("发生错误：" + jqXHR.status);
                            }
                        });

                    } else {
                        var updatebtn = $("<a class='btn btn-xs btn-info' style='margin-right:5px'>编辑</a>");
                        var movebtn = $("<a class='btn btn-xs btn-warning' style='margin-right:5px'>移动</a>");
                        var delbtn = $("<a class='btn btn-xs btn-danger' style='margin-right:5px'>删除</a>");
                        var signbtn = $("<div class='pull-right'></div>"),
                            del = true,
                            update = true,
                            move = true;
                        if ($this.Options.editable !== false) {
                            if ($this.Options.editable !== true) {
                                if ($this.Options.editable.update === false) {
                                    update = false;
                                }
                                if ($this.Options.editable.move === false) {
                                    move = false;
                                }
                                if ($this.Options.editable.del === false) {
                                    del = false;
                                }
                            }
                            if (update) {
                                signbtn.append(updatebtn);
                                updatebtn.attr("data-signid", signdata.signid);
                                updatebtn.addClass("picsign_bedit_" + $this.ID);
                            }
                            if (move) {
                                signbtn.append(movebtn);
                                movebtn.attr("data-signid", signdata.signid);
                                movebtn.addClass("picsign_bmove_" + $this.ID);
                            }
                            if (del) {
                                signbtn.append(delbtn);
                                delbtn.attr("data-signid", signdata.signid);
                                delbtn.addClass("picsign_bdel_" + $this.ID);
                            }
                        }
                        var line = '';

                        for (let i = 0; i < signdata.msg.msg11.length; i += 1) {
                            line += '规格：'+signdata.msg.msg11[i].select2+' ; 设备端口号：' + signdata.msg.msg11[i].paramCodeId + ' ; 上限：' + signdata.msg.msg11[i].limitTop + ' ; 下限：' + signdata.msg.msg11[i].limitLow + '<br/>'
                        }
                        // console.log('dompopoverelse',line);
                        
                        sign.webuiPopover($.extend({}, webuiPopoverSetting, {
                            title: signdata.msg.pdxname + signbtn.prop("outerHTML"),
                            width: 700,
                            height: 300,
                            content: "配电箱名称：" + signdata.msg.pdxname + '<br/>' + "设备名称：" + signdata.msg.sbname + '<br/>'+ "设备编号：" + signdata.msg.sbbh + '<br/>' +line,

                            onShow: function () {
                                $this.movecancel();
                            }
                        }));
                    }

                }
            }
        }
        //DOM更新
        domupdate(signdata) {
            if (signdata.signid) {
                this.domget(signdata.signid).css({
                    "left": signdata.left,
                    "top": signdata.top,
                    "display": "block"
                });
                this.dompopover(signdata, false);
            }
        }
        //DOM删除
        domdel(signid) {
            // alert(signid)
            this.domget(signid).webuiPopover('destroy');
            this.domget(signid).remove();
        }
        //DOM获取
        domget(signid) {
            if (signid) {
                return $("." + this.Options.signclass + "[data-signid='" + signid + "']");
            } else {
                return $("." + this.Options.signclass);
            }
        }
        //取消移动
        movecancel() {
            this.signMoveId = null;
            this.$el.css("cursor", "");
        }
        //数据初始化
        datainit() {
            this.dataset(this.Options.signdata, false);
        }
        datainit2() {
            this.dataset2(this.Options.signdata, false);
        }
        //数据设置(单个或数组)
        dataset(signdata, istrigger) {
            var $this = this;
            if (signdata) {
                if (Array.isArray(signdata)) {
                    for (var i = 0; i < signdata.length; i++) {
                        $this.dataset_one(signdata[i], istrigger);
                    }
                } else {
                    $this.dataset_one(signdata, istrigger);
                }
            }
        }
        dataset2(signdata, istrigger) {
            var $this = this;
            if (signdata) {
                if (Array.isArray(signdata)) {
                    for (var i = 0; i < signdata.length; i++) {
                        $this.dataset_one2(signdata[i], istrigger);
                    }
                } else {
                    $this.dataset_one2(signdata, istrigger);
                }
            }
        }
        //数据设置(单个)
        dataset_one(signdata, istrigger) {
            var $this = this;
            if (signdata) {
                if (signdata.signid) {
                    //修改
                    var data = $this.dataget(signdata.signid);
                    var copy = {
                        left: data.left,
                        top: data.top,
                        msg: data.msg
                    };
                    if (data) {
                        if (signdata.left) {
                            data.left = signdata.left;
                        }
                        if (signdata.top) {
                            data.top = signdata.top;
                        }
                        if (signdata.msg) {
                            data.msg = signdata.msg;
                        }
                        if ($this.event("beforeupdate", data, istrigger)) {
                            $this.domupdate(data);
                            $this.event("onupdate", data, istrigger);
                        } else {
                            data.left = copy.left;
                            data.top = copy.top;
                            data.msg = copy.msg;
                        }
                    }
                } else {
                    //添加
                    if (signdata.left && signdata.top && signdata.msg) {
                        var indexId = $this.Data.length;
                        signdata['signid'] = untrue_guid() + "_" + indexId;
                        // console.log('66666',signdata);
                        
                        if ($this.event("beforeadd", signdata, istrigger)) {
                            $this.Data.push(signdata);
                            $this.domadd(signdata);
                            $this.event("onadd", signdata, istrigger);
                        }
                    }
                }
            }
        }
        dataset_one2(signdata, istrigger) {
            var $this = this;
            if (signdata) {
                if (signdata.signid) {
                    //修改
                    var data = $this.dataget(signdata.signid);
                    var copy = {
                        left: data.left,
                        top: data.top,
                        msg: data.msg
                    };
                    if (data) {
                        if (signdata.left) {
                            data.left = signdata.left;
                        }
                        if (signdata.top) {
                            data.top = signdata.top;
                        }
                        if (signdata.msg) {
                            data.msg = signdata.msg;
                        }
                        if ($this.event("beforeupdate", data, istrigger)) {
                            $this.domupdate(data);
                            $this.event("onupdate", data, istrigger);
                        } else {
                            data.left = copy.left;
                            data.top = copy.top;
                            data.msg = copy.msg;
                        }
                    }
                } else {
                    //添加
                    if (signdata.left && signdata.top && signdata.msg) {
                        var indexId = $this.Data.length;
                        signdata['signid'] = untrue_guid() + "_" + indexId;
                        // console.log('66666',signdata);
                        
                        if ($this.event("beforeadd", signdata, istrigger)) {
                            $this.Data.push(signdata);
                            $this.domadd2(signdata);
                            $this.event("onadd", signdata, istrigger);
                        }
                    }
                }
            }
        }
        //数据获取
        dataget(signid) {
            var $this = this;
            for (var i = 0; i < $this.Data.length; i++) {
                if ($this.Data[i].signid === signid) {
                    return $this.Data[i];
                }
            }
            return null;
        }
        //数据删除
        datadel(signid, istrigger) {
            // alert('haha')
            var $this = this;
            for (var i = 0; i < $this.Data.length; i++) {
                if ($this.Data[i].signid === signid) {
                    layer.confirm('是否删除标注？', {
                        icon: 0,
                        title: '删除标注'
                    }, function (index) {
                        layer.close(index);
                        $.ajax({
                            type: "POST",
                            url: posturl+'/device/deletedevice',
                            dataType: "json",
                            data: {
                                deviceId: $this.Data[i].msg.sbbh,
                                token:sessionStorage.getItem('token')
                    
                            },
                            success: function (data) {
                                if (data.res=='true') {
                                    if ($this.event("beforedel", $this.Data[i], istrigger)) {
                                        $this.domdel(signid);
                                        $this.event("ondel", $this.Data[i], istrigger);
                                        $this.Data.splice(i, 1);
                                    }
                                    layer.msg('删除成功');
                                }else{
                                    layer.msg(data.msg);
                                }
  
                            },
                            error: function (jqXHR) {
                                alert("发生错误：" + jqXHR.status);
                            }
                        });
  
                    });
                    break;
                } else {
                    continue;
                }
            }
        }
        //事件
        event(e, signdata, istrigger) {
            var $this = this;
            if (istrigger !== false) {
                var fn;
                switch (e) {
                    case "beforeadd":
                        fn = $this.Options.beforeadd;
                        break;
                    case "onadd":
                        fn = $this.Options.onadd;
                        break;
                    case "beforeupdate":
                        fn = $this.Options.beforeupdate;
                        break;
                    case "onupdate":
                        fn = $this.Options.onupdate;
                        break;
                    case "beforedel":
                        fn = $this.Options.beforedel;
                        break;
                    case "ondel":
                        fn = $this.Options.ondel;
                        break;
                    default:
                        fn = null;
                }
                if (fn && typeof fn === 'function') {
                    if (fn(signdata) === false) {
                        return false;
                    }
                }
            }
            return true;
        }
        //获取标记数据
        getData() {
            return this.Data;
        }
        //添加标注
        addSign(signdata, istrigger) {
            if (signdata) {
                this.dataset(signdata, istrigger);
            }
        }
        //切换显示状态
        toggle() {
            this.domget().toggle();
        }
    }
})(jQuery);